<?php
/**
 * Created by PhpStorm.
 * User: SATA
 * Date: 2017/10/6
 * Time: 15:45
 */

namespace app\admin\controller;


class Appointment extends Admin
{
    /**
     * 订单管理首页
     */
    public function index(){
        $data=input();
        $map = [];
        if(isset($data['mobile'])){
            $map['mobile']  = array('like', '%'.(string)input('title').'%');
        }
        if(isset($data['status'])){
            $map['status'] = input('status');
            $status = $map['status'];
        }else{
            $status = null;
            $map['status'] = array('in', '0,1,2');
        }
        if ( isset($data['time-start']) ) {
            $map['appoint_time'][] = array('> time',input('time-start'));
        }
        if ( isset($data['time-end']) ) {
            $map['appoint_time'][] = array('<= time',input('time-end'));
        }
        if ( isset($data['teacher']) ) {
            $map['name'] = input('teacher');
        }
        $list = Model('Appointment')->lists($map);
        $page = $list->render();
        $this->assign('status',$status);
        $this->assign('_list',$list);
        $this->assign('_page',$page);
        return $this->fetch();
    }

    /**
     *回收站
     */
    public function recycle(){
        $list = Model('Appointment')->lists(["status"=>-1]);
        $page = $list->render();
        $this->assign('_list',$list);
        $this->assign('_page',$page);
        return $this->fetch();
    }

    /**
     * 清空回收站
     * @author wapai   邮箱:wapai@foxmail.com
     */
    public function clear(){
        $model = \think\Loader::model('appointment');
        $res = $model->remove();
        if($res !== false){
            $this->success('清空回收站成功！');
        }else{
            $this->error('没有订单被清除！');
        }
    }


    /**
     * 订单状态修改
     */
    public function changeStatus($method=null){
        $data=input('id/a');
        $id = array_unique($data);
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        error_note($map);
        switch ( strtolower($method) ){
            case 'signappoint':
                $this->forbid('appointment', $map,['success'=>'成功标记为问题订单','error'=>'标记失败'] );
                break;
            case 'processappoint':
                $this->process('appointment', $map,['success'=>'成功处理订单','error'=>'标记失败'] );
                break;
            case 'deleteappoint':
                $this->delete('appointment', $map,['success'=>'成功删除','error'=>'删除失败']);
                break;
            case 'resumeappoint':
                $this->resume('appointment', $map,['success'=>'成功还原','error'=>'还原失败']);
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     * 处理订单
     * @param string $model 模型名称,供D函数使用的参数
     * @param array  $where 查询时的where()方法的参数
     * @param array  $msg   执行正确和错误的消息 array('success'=>'','error'=>'', 'url'=>'','ajax'=>false)
     *                     url为跳转页面,ajax是否ajax方式(数字则为倒数计时秒数)
     */
    private function process($model , $where = array() , $msg = array( 'success'=>'状态处理成功！', 'error'=>'状态处理失败！')){
        $data    =  array('status' => 2);
        $this->editRow(   $model , $data, $where, $msg);
    }
}